local skel = fk.CreateSkill {
  name = "rmt__yiku",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["rmt__yiku"] = "易苦",
  [":rmt__yiku"] = "锁定技，你额定回合的摸牌阶段额定摸牌数改为一张；你因此每少摸一张牌，便可弃置一名角色的一张牌或移动场上一张牌，然后场上红黑牌的数量因此相等，当前回合结束时你执行一个额外回合。",

  ["@@rmt__yiku-turn"] = "易苦:成功",
  ["#rmt__yiku-throw"] = "弃置1名角色1张牌",
  ["#rmt__yiku-move"] = "移动场上一张牌",
  ["#rmt__yiku-choice"] = "易苦：请选择一项执行（当前第 %arg 次，共 %arg2 次）",
}

skel:addEffect(fk.DrawNCards, {
  anim_type = "control",
  priority = 0.99, -- 晚于1技能
  can_trigger = function(self, event, target, player, data)
    return target == player and not player:insideExtraTurn() and player:hasSkill(skel.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local x = data.n - 1
    data.n = 1
    if x < 0 then return end
    local extraCheck
    for i = 1, x do
      local choices = {}
      if #room:canMoveCardInBoard() > 0 then
        table.insert(choices, "#rmt__yiku-move")
      end
      local throwTars = table.filter(room.alive_players, function (p) return not p:isNude() end)
      if #throwTars > 0 then
        table.insert(choices, "#rmt__yiku-throw")
      end
      if #choices == 0 then break end
      table.insert(choices, "Cancel")
      local choice = room:askToChoice(player, { choices = choices, skill_name = skel.name, prompt = "#rmt__yiku-choice:::"..i..":"..x})
      if choice == "Cancel" then
        break
      elseif choice == "#rmt__yiku-move" then
        local tars = room:askToChooseToMoveCardInBoard(player, {prompt = choice, skill_name = skel.name, cancelable = false})
        if #tars == 2 then
          room:askToMoveCardInBoard(player, { target_one = tars[1], target_two = tars[2], skill_name = skel.name})
        end
      else
        local tos = room:askToChoosePlayers(player, {
          targets = throwTars, min_num = 1, max_num = 1, prompt = choice, skill_name = skel.name, cancelable = false
        })
        local to = tos[1]
        local cid = room:askToChooseCard(player, { target = to, flag = "he", skill_name = skel.name })
        -- 仅选择弃置装备区牌的情况下检测是否进行额外回合
        if room:getCardArea(cid) == Player.Equip then
          extraCheck = true
        end
        room:throwCard(cid, skel.name, to, player)
      end
      if player.dead then return end
    end
    if not extraCheck then return end
    -- 在完成所有操作后才检测黑红颜色相同
    local red, black = 0, 0
    for _, p in ipairs(room.alive_players) do
      for _, id in ipairs(p:getCardIds("ej")) do
        if Fk:getCardById(id).color == Card.Red then
          red = red + 1
        elseif Fk:getCardById(id).color == Card.Black then
          black = black + 1
        end
      end
    end
    if red == black then
      room:setPlayerMark(player, "@@rmt__yiku-turn", 1)
      player:gainAnExtraTurn(true, skel.name)
    end
  end,
})



return skel
